<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    /* 
    子组件向父组件传值：
    1. 在父组件中的子组件标签上绑定一个自定义属性，自定义属性值必须为一个函数
    2. 在子组件中可以通过调用父组件传递的函数实现函数传参 
    */
    class App extends React.Component{
        state={
            num:""
        }
        render(){
            return (
                <div>
                    <h1>{this.state.num}</h1>
                    <Child getNum={(num)=>{
                        console.log("调用我")
                        console.log(num)
                        this.setState({
                            num
                        })
                    }}/>
                </div>
            )
        }
    }
    class Child extends React.Component{
         state = {
            num:0
         }
        render(){
            console.log(this.props)
            return (
                <div>
                        <h1>子组件</h1>   
                        <button onClick={()=>{
                            this.setState((state)=>{
                                return {
                                    num:state.num+1
                                }
                            },()=>{
                                this.props.getNum(this.state.num)
                            })
                          
                        }}>向父组件传值</button>
                </div>
            )
        }
    }
    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>